package com.aerospace.util;

import java.io.*;

/**
 * @ClassName FileUtils
 * @Description TODO
 * @Author Aero
 * @Date 2019/10/7 16:58
 * @Version 1.0
 **/
public class FileUtils {
    public FileUtils() {
    }

    public static byte[] read(String path) throws IOException {
        int base_size = 1024;
        File file = new File(path);
        if (!file.exists()) {
            return null;
        } else {
            FileInputStream fis = new FileInputStream(file);
            byte[] dataByte = new byte[base_size];
            ByteArrayOutputStream out = new ByteArrayOutputStream(base_size);

            int len;
            while((len = fis.read(dataByte)) != -1) {
                out.write(dataByte, 0, len);
            }

            byte[] content = out.toByteArray();
            fis.close();
            out.close();
            return content.length == 0 ? null : content;
        }
    }

    public static void write(String path, byte[] data) throws IOException {
        System.out.println(path);
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data);
        fos.flush();
        fos.close();
    }
}
